package structures;
import java.io.Serializable;
import java.util.ArrayList;

import Items.*;
/**
 * Inventory: holds Item objects wallet amount, held by Spaceship/Tradepost
 *
 * @author Branon Lyle
 * @param Item
 */
@SuppressWarnings("serial")
public class Inventory extends ArrayList<Item> implements Serializable{
	
	public Inventory(){
		super();
	}
	
	public Inventory(int[] itemAmounts, int wallet){
		super();
		this.wallet = wallet;
		this.add(new Water(itemAmounts[0]));
        this.add(new Sandwich(itemAmounts[1]));
        this.add(new BandAid(itemAmounts[2]));
        this.add(new Tylenol(itemAmounts[3]));
        this.add(new DuckTape(itemAmounts[4]));
        this.add(new CryoLiquid(itemAmounts[5]));
        this.add(new Gun(itemAmounts[6]));
        this.add(new SpaceOxen(itemAmounts[7]));
	}
    
    private int wallet;
    
    /**
     * Wallet Accessor
     * @return wallet
     */
    public int getWallet(){
    	return wallet;
    }
    
    /**
     * Wallet Mutator
     * @param wallet the intended value of the wallet for this Inventory
     */
    public void setWallet(int wallet) {
        this.wallet = wallet;
    }
    
    /**
     * Add/subtract some amount from the wallet of this Inventory
     * @param amount
     */
    public void modifyWallet(int amount) {
        this.wallet += amount;
    }
    
    /**
     * Change the quantity of the Item at the given index but change amount
     * @param i the index of the Item object to modify the quantity of
     * @param change the amount the modify the quantity of
     */
    public void changeQuantity(int i, int change) {
        this.get(i).modifyQuantity(change);
    }
    
    public int getQuantity(int i){
    	return this.get(i).getQuantity();
    }
    
    /**
     * Allows the manual set of a quantity for a specific item in the inventory
     * @param i
     * @param setTo
     */
    public void setQuantity(Item i, int setTo){
    	get(indexOf(i)).setQuantity(setTo);
    }
    
    /**
     * Builds the initial inventory for the Spaceship
     *  - Items have zero quantities
     */
    public void buildSpaceshipInventory() {
        wallet = 2000;
        this.add(new Water());
        this.add(new Sandwich());
        this.add(new BandAid());
        this.add(new Tylenol());
        this.add(new DuckTape());
        this.add(new CryoLiquid());
        this.add(new Gun());
        this.add(new SpaceOxen());
    }
   
    /**
     * Builds the inventory for the Trade Post
     *  - Items have nonzero initial quantities
     */
    public void buildTradePostInventory() {
        wallet = 100000;
        this.add(new Water(1000));
        this.add(new Sandwich(1000));
        this.add(new BandAid(1000));
        this.add(new Tylenol(1000));
        this.add(new DuckTape(1000));
        this.add(new CryoLiquid(1000));
        this.add(new Gun(1000));
        this.add(new SpaceOxen(1000));
    }
    
    /**
     * Calculates the total amount of kleptons this inventory takes up
     *  - derived from the total of the klepton amounts of the individual items
     * @return kleptons the total number of kleptons in this Inventory
     */
    public int calculateKleptons() {
        int kleptons = 0;
        for (int i = 0; i < size(); i++) {
            kleptons = kleptons + get(i).getKlepton();
        }
        return kleptons;
    }
    
    /**
     * Return the quantity of the inputed Item from this Inventory
     * @param i the Item to get the quantity of
     * @return quantity of the inputed Item in this Inventory
     */
    public int getQuantity(Item i) {
        return get(indexOf(i)).getQuantity();
    }
    
    /**
     * Add the quantity to the corresponding Item in this Inventory
     * @param i the Item (essentially serves as type)
     * @param quantity the amount to add to quantity of the Item in Inventory
     */
    public void addItem(Item i, int quantity) {
        get(indexOf(i)).modifyQuantity(quantity);
    }
    
    /**
     * Subtracts the inputed quantity from the corresponding Item in this Inventory
     * @param i the Item (essentially serves as type)
     * @param quantity the amount to subtract from quantity of the Item in Inventory
     */
    public void removeItem(Item i, int quantity) {
        addItem(i, -quantity);
    }
    
    /**
     * Checks to see if this Inventory has enough money in its wallet to justify spending
     * the inputed amount
     * @param spent
     * @return true if the amount spent is less than the wallet
     */
    public boolean checkWallet(int spent){
    	if(wallet > spent)
    		return true;
    	else
    		return false;
    }  
    
    /**
     * Returns String representation of this Inventory
     */
    public String toString() {
        String result = "";
        for (int i = 0; i < size(); i++) {
            Item item = get(i);
            result += item.toString() + "\n";
            result += "Quantity: " + item.getQuantity() + "\n";
            //result += "Kleptons: " + item.getKlepton() + "\n";
            //result += "Base Price: " + item.getBasePrice() + "\n\n";
            result += "\n";
        }
        result += "Wallet: " + wallet;
        return result;
    }
}
